package supermarket.dao.impl;

import java.util.List;

import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import supermarket.dao.ProductDao;
import supermarket.model.Product;

public class ProductDaoImpl extends HibernateDaoSupport implements ProductDao {

	@Override
	public Product findProduct(Integer id) {
		return (Product)getHibernateTemplate().get(Product.class, id);
	}
	
	@Override
	public List findProductByName(String name) {
		List productList = this.getHibernateTemplate().find(
			"FROM Product AS product WHERE product.name LIKE ?", name + "%");
		if(productList.isEmpty()) {
			return null;
		}
		return productList;
	}
	
	@Override
	public List findProductByCategory(String category) {
		List productList = this.getHibernateTemplate().find(
			"FROM Product AS product WHERE product.category LIKE ?", category + "%");
		if(productList.isEmpty()) {
			return null;
		}
		return productList;
	}
	
	@Override
	public List findAll() {
		return (List)getHibernateTemplate().find("FROM Product");
	}
	
	@Override
	public void addProduct(Product product) {
		getHibernateTemplate().save(product);
	}
	
	@Override
	public void addProductByName(String name) {
		getHibernateTemplate().save(new Product(name));
	}
	
	@Override
	public void updateProduct(Product product) {
		getHibernateTemplate().update(product);
	}
	
	@Override
	public void removeProduct(Product product) {
		getHibernateTemplate().delete(product);
	}
	
}